//
//  HJZPhotoView.m
//  HJZWeiBo
//
//  Created by 蒋晨成 on 16/6/1.
//  Copyright © 2016年 蒋晨成. All rights reserved.
//

#import "HJZPhotoView.h"
#import <UIImageView+WebCache.h>
#define HJZPhotoH ((kWidth - 70) / 3)

@interface HJZPhotoView ()

@property (nonatomic, weak) UIImageView *gifView;

@end

@implementation HJZPhotoView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIImage *gif = [UIImage imageNamed:@"timeline_image_gif"];
        UIImageView *gifView = [[UIImageView alloc] initWithImage:gif];
        [self addSubview:gifView];
        
        self.gifView = gifView;
    }
    return self;
}

- (void)setThumbnail_pic:(NSString *)thumbnail_pic
{
    _thumbnail_pic = thumbnail_pic;
    
    self.gifView.hidden = ![thumbnail_pic hasSuffix:@".gif"];
    
    [self sd_setImageWithURL:[NSURL URLWithString:thumbnail_pic]
            placeholderImage:[UIImage imageNamed:@"timeline_image_placeholder"]];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.gifView.layer.anchorPoint = CGPointMake(1, 1);
    self.gifView.layer.position = CGPointMake(self.bounds.size.width, self.bounds.size.height);
}

- (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect {
    CGImageRef sourceImageRef = [image CGImage];
    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
    return newImage;
}
@end
